# Maintainer: Alexander F. Rødseth <xyproto@archlinux.org>
# Contributor: phrakture <aaronmgriffin@gmail.com>
# Contributor: Fazlul Shahriar <>
# Contributor: Chris Brannon <cmbrannon79@gmail.com>

pkgname=plan9port
_commit=984c2824e3569479bace65bdaf9e78a2eb36dd58 # master
pkgver=20230913
pkgrel=1
pkgdesc='Ports of applications from Plan 9'
arch=(loong64 x86_64)
url='https://swtch.com/plan9port/'
url='https://9fans.github.io/plan9port/'
license=(custom)
depends=(fontconfig fuse2 libxt xorg-server)
makedepends=(gendesk git)
provides=(plan9)
install=plan9.install
options=(staticlibs !zipman)
source=("git+https://github.com/9fans/plan9port#commit=$_commit"
        acme.png acme.sh plan9.sh)
b2sums=('SKIP'
        '983106b7a91afadae0f3aff5ff69338aa80d7eb6f9b65a95a31c963f162f6fb2dea748e5da698e0bd14d4343fe17281f55b9a1966fcba1c497fbd4bd855caf6c'
        '6bd5abac872d578523fee4d7287aa9df7faecccefab92377a871596f1526f3a26d3e89098aef3bb3e3773b2de6f5f63be6cb13134b1cbc66be9763403c344598'
        '3de66befb0a864a5e1e01f5700bb14bc927bec4738e394e0b73b09f7322bb738d51c611376b3360883e20b605df15bff6fb0fd7850545e35d5b92ec3990469d6')

pkgver() {
 cd $pkgname
 git log -n1 --pretty='format:%cd' --date=format:'%Y%m%d' $_commit
}

prepare() {
  # Generate a desktop shortcut for the Acme editor
  gendesk -f \
    --name Acme \
    --pkgname acme \
    --genericname Editor \
    --comment 'Editor from Plan9' \
    --categories 'Application;Development;TextEditor'

  cd $pkgname

  # Use _DEFAULT_SOURCE instead of _BSD_SOURCE
  grep -rl _BSD_SOURCE | xargs sed -i s':_BSD_SOURCE:_DEFAULT_SOURCE:g'

  # Use the Gentoo way for fixing hardcoded paths
  grep --null -l -r /usr/local/plan9 | xargs --null sed -i "s!/usr/local/plan9!/usr/lib/plan9!g"

  # Stop 9l from giving an 'undefined reference error' given no arguments, ref #55640
  sed -i '1i[ "$1" == "" ] && exit 1' bin/9l
}

build() {
  cd $pkgname
  export CFLAGS+=' -ffat-lto-objects -w'
  ./INSTALL -b
}

package() {
  cd $pkgname
  install -Dm755 ../plan9.sh "$pkgdir/etc/profile.d/plan9.sh"
  d=usr/lib
  install -d "$pkgdir/$d"
  install -d "$pkgdir/usr/share/doc/$pkgname"
  cp -r "$srcdir/$pkgname" "$pkgdir/$d/plan9"
  cd "$pkgdir/$d/plan9"
  ./INSTALL -c -r "$pkgdir/$d/plan9"
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  # Clean up
  rm -rf .hg/
  rm -f .hgignore .hgtags
  find . -name '.cvsignore' -delete
  rm -f config install.log install.sum install.txt configure Makefile INSTALL \
    LICENSE

  # Package text files
  for i in CHANGES CONTRIBUTING.md CONTRIBUTORS README.md; do
    install -m644 $i "$pkgdir/usr/share/doc/$pkgname"
    rm -f $i
  done

  # Decompress the plan9 man pages
  for i in "$(find "$pkgdir/$d/plan9/man" -type f)"; do
    if [ "${i##*.}" = "gz" ]; then
      gunzip "$i"
    fi
  done

  # Once more
  PLAN9=/usr/lib/plan9
  grep --null -l -r "$pkgdir$PLAN9" | xargs --null sed -i "s!$pkgdir$PLAN9!${PLAN9}!g" || true
  grep --null -l -r "/build/plan9port/pkg/plan9port/$PLAN9" | xargs --null sed -i "s!/build/plan9port/pkg/plan9port/$PLAN9!${PLAN9}!g" || true

  # Acme
  cd "$srcdir"
  install -Dm644 acme.desktop "$pkgdir/usr/share/applications/acme.desktop"
  install -Dm755 acme.sh "$pkgdir/usr/bin/acme"
  install -Dm644 acme.png "$pkgdir/usr/share/pixmaps/acme.png"

  # Remove git repository
  rm -rf "$pkgdir/usr/lib/plan9/.git"
}
